package com.lxy.demo.servlet;

import com.lxy.demo.entity.City;
import com.lxy.demo.entity.Condition;
import com.lxy.demo.entity.ToursInfo;
import com.lxy.demo.service.CityService;
import com.lxy.demo.service.CityServiceImpl;
import com.lxy.demo.service.ToursInfoService;
import com.lxy.demo.service.ToursInfoServiceImpl;
import com.lxy.demo.utils.PageBean;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet(name = "ServletMain", urlPatterns = "/main")
public class ServletMain extends HttpServlet {
    static CityService cityService = new CityServiceImpl();
    static ToursInfoService touService = new ToursInfoServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String cmd = request.getParameter("cmd");
        if (cmd != null && cmd.equals("register")){
            register(request,response);
        }
        if (cmd != null && cmd.equals("load")){
            load(request,response);
        }
        if (cmd != null && cmd.equals("main")){
            main(request,response);
        }
        if (cmd != null && cmd.equals("del")){
            del(request,response);
        }
        if (cmd != null && cmd.equals("upd")){
            upd(request,response);
        }
        if (cmd != null && cmd.equals("update")){
            update(request,response);
        }
        if (cmd != null && cmd.equals("full")){
            full(request,response);
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    //主页显示 分页查询 查询条件回显
    public void main (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //取
        Map<String, String[]> map = request.getParameterMap();
        Condition condition = new Condition();
        try {
            BeanUtils.populate(condition,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //获取前端传递的页数
        String strP = request.getParameter("p");    //
        Integer p = 1;
        if (strP != null && !strP.isEmpty()){
            p = Integer.valueOf(strP);

        }

        //调
        PageBean pb = touService.findInfoBySome(condition, p);

        //存(这里要用到两个值，一个是回显用到的condition,一个是包含了查询数据的pageBean)
        request.setAttribute("condition",condition);
        request.setAttribute("pb",pb);

        //转
        request.getRequestDispatcher("page/main.jsp").forward(request,response);
    }

    //添加操作
    public void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //获取所有参数
        Map<String, String[]> map = request.getParameterMap();
        ToursInfo toursInfo = new ToursInfo();

        //一键存值
        try {
            BeanUtils.populate(toursInfo,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        boolean b = touService.addInfo(toursInfo);
        if (b){
            //response.getWriter().write("注册成功");
            request.setAttribute("register","ok");
        }else {
            //response.getWriter().write("注册失败");
            request.setAttribute("register","no");
        }

        main(request,response);

    }

    //下拉框加载
    public void load(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        List<City> city = cityService.findCity();
        request.getSession().setAttribute("city",city);
        response.sendRedirect(request.getContextPath()+"/main?cmd=main");
    }

    //删除操作
    public void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String id = request.getParameter("id");
        boolean b = touService.delInfo(Integer.valueOf(id));
        if (b){
            request.setAttribute("del","ok");
        }else {
            request.setAttribute("del","no");
        }
        main(request,response);
    }

    //修改页面
    public void upd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String id = request.getParameter("id");
        ToursInfo one = touService.findOne(Integer.valueOf(id));
        request.setAttribute("id",id);
        request.setAttribute("one",one);
        request.getRequestDispatcher("page/update.jsp").forward(request,response);
    }

    //修改操作
    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String id = request.getParameter("id");
        Map<String, String[]> map = request.getParameterMap();
        ToursInfo toursInfo = new ToursInfo();

        try {
            BeanUtils.populate(toursInfo,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        System.out.println(toursInfo);
        System.out.println(id);

        //调
        boolean b = touService.update(Integer.valueOf(id), toursInfo);

        //存
        if (b){
            request.setAttribute("upd","ok");
        }else {
            request.setAttribute("upd","no");
        }

        //转
        main(request,response);
    }

    public void full(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String id = request.getParameter("id");
        ToursInfo one = touService.findOne(Integer.valueOf(id));
        request.setAttribute("id",id);
        request.setAttribute("one",one);
        request.getRequestDispatcher("page/full.jsp").forward(request,response);
    }
}
